/* LINUXLB.C
 * Copyright (c) 2007 MissingNODE 
 * Based on GPL OpenQM Code
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 * 
 * START-HISTORY:
 * Oct-10-2007 Initial Revision
 * END-HISTORY
 *
 *
 * START-DESCRIPTION:
 *
 *
 * END-DESCRIPTION
 *
 * START-CODE
 */

#include <tm.h>

#include <pwd.h>
#include <time.h>

#include <crypt.h>

/* ======================================================================
   filelength64()  -  Return file size in bytes                           */

int64 filelength64(fd)
   int fd;
{
 struct stat statbuf;

 fstat(fd, &statbuf);
 return statbuf.st_size;
}

/* ======================================================================
   IsAdmin()  -  Is this user an administrator at the o/s level?          */

bool IsAdmin(void)
{
 return (getuid() == 0); 
}

/* ======================================================================
   itoa()  -  Convert integer to string                                   */

char * itoa(value, string, radix)
   int value;
   char * string;
   int radix;        /* Ignored */
{
 sprintf(string, "%d", value);
 return string;
}

/* ======================================================================
   ltoa()  -  Convert long integer to string                              */

char * ltoa(value, string, radix)
   long int value;
   char * string;
   int radix;        /* Ignored */
{
 sprintf(string, "%ld", value);
 return string;
}

/* ======================================================================
   GetUserName()  -  Return user name for logged in user.                 */

bool GetUserName(name, bytes)
   char * name;
   unsigned long int * bytes;  /* Buffer size  -  updated to actual size on exit */
{
 char * p;
 int n = 0;

//#ifdef LINUX
// p = (char *)cuserid(NULL);     /* 0348 Was getlogin() */
//#else
 struct passwd * pw;

 pw = getpwuid(getuid());
 p = (pw == NULL)?NULL:(pw->pw_name);
//#endif

 if (p != NULL)
  {
   n = strlen(p);
   if (*bytes >= n) n = *bytes - 1;
   memcpy(name, p, n);
  }
 *(name + n) = '\0';
 *bytes = n;

 return TRUE;
}

/* ======================================================================
   Sleep()  -  Sleep for period in milliseconds                           */

void Sleep(n)
   long int n;
{
 struct timespec period;
 struct timespec remaining;

 period.tv_sec = n / 1000;
 period.tv_nsec = (n % 1000) * 1000000;
 nanosleep(&period, &remaining);
}

/* END-CODE */
